#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.basic.module.menu.controller;

import ${package}.basic.module.menu.service.MenuService;
import ${package}.basic.module.user.service.UserService;
import ${package}.common.bean.po.Menu;
import ${package}.common.bean.po.User;
import ${package}.common.bean.vo.AddMenuVo;
import ${package}.common.bean.vo.DisableOrEnableMenuVo;
import ${package}.common.bean.vo.JsonResult;
import ${package}.common.bean.vo.QueryBean;
import ${package}.common.bean.vo.SubPermission;
import ${package}.common.bean.vo.UpdateMenuVo;
import ${package}.common.bean.vo.ViewTree;
import ${package}.common.emum.ReturnCodeEnum;
import ${package}.common.utils.SessionUtil;
import ${package}.common.utils.SnowFlakeUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Description: 菜单
 *
 * @author wupanhua
 * @date 2019/8/6 15:28
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Slf4j
@RestController
@RequestMapping("/yk_api/menu/")
@AllArgsConstructor
@Api(value = "菜单")
public class MenuController {

    /**
     * 菜单Service
     */
    private MenuService menuService;
    /**
     * 用户Service
     */
    private UserService userService;


    /**
     * Description:
     * <获取分页列表>
     * @author wupanhua
     * @date 17:24 2019/8/6
     * @param queryBean 1
     * @return JsonResult
     **/
    @PostMapping("menuList")
    @ApiOperation(value = "获取分页列表",notes = "获取分页列表")
    @ApiImplicitParam(name = "queryBean",value = "查询条件",dataType = "QueryBean",paramType = "body",required = true)
    public JsonResult<PageInfo<Menu>> menuList(@RequestBody @Valid QueryBean queryBean){

        PageInfo<Menu> pageInfo = menuService.queryMeunListByLimit(queryBean);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, pageInfo);
    }

    /**
     * Description:
     * <新增菜单>
     * @author wupanhua
     * @date 17:24 2019/8/6
     * @param addMenuVo 1
     * @return JsonResult
     **/
    @PutMapping("addMenu")
    @ApiOperation(value = "新增菜单",notes = "新增菜单")
    @ApiImplicitParam(name = "addMenuVo",value = "新增菜单实体类",dataType = "AddMenuVo",paramType = "body",required = true)
    public JsonResult<Menu> addMenu(@RequestBody @Valid AddMenuVo addMenuVo){

        Menu menu = new Menu();

        BeanUtils.copyProperties(addMenuVo,menu);

        menu.setId(SnowFlakeUtil.getID());
        menuService.addMenu(menu);

        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, menu);
    }

    /**
     * Description:
     * <获取当前用户的菜单列表>
     * @author wupanhua
     * @date 17:25 2019/8/6
//     * @param session 1
     * @return JsonResult
     **/
    @GetMapping("ownMenu")
    @ApiOperation(value = "获取当前用户的菜单列表",notes = "获取当前用户的菜单列表")
    @ApiImplicitParam(name = "username",value = "用户名",paramType = "query",required = true)
    public JsonResult<List<Menu>> queryMenuByCurrentUser(@RequestParam String username){

        User user = userService.findUserByUsername(username);
        List<Menu> menus = menuService.queryMenuByCurrentUser(user);

        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, menus);
    }

    /**
     * Description:
     * <获取所有的菜单>
     * @author wupanhua
     * @date 17:25 2019/8/6
     * @return JsonResult
     **/
    @GetMapping("all")
    @ApiOperation(value = "获取所有的菜单",notes = "获取所有的菜单")
    public JsonResult<List<Menu>> allMenu(){
        List<Menu> menus = menuService.queryAllMenu();
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, menus);
    }

    /**
     * Description:
     * <iView的树形插件>
     * @author wupanhua
     * @date 17:26 2019/8/6
     * @return JsonResult<java.util.List<ViewTree>>
     **/
    @GetMapping("iview/tree")
    @ApiOperation(value = "iView的树形插件",notes = "iView的树形插件")
    public JsonResult<List<ViewTree>> treeMenu(){
        List<ViewTree> root = menuService.treeMenu();
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, root);
    }


    /**
     * Description:
     * <根据菜单主键获取下级权限列表>
     * @author wupanhua
     * @date 17:27 2019/8/6
     * @param menuKey 1
     * @param roleKey 2
     * @return JsonResult
     **/
    @GetMapping("subPermission")
    @ApiOperation(value = "根据菜单主键获取下级权限列表",notes = "根据菜单主键获取下级权限列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuKey",value = "菜单主键",dataType = "Long",paramType = "query",required = true),
            @ApiImplicitParam(name = "roleKey",value = "角色主键",dataType = "Long",paramType = "query",required = true)})
    public JsonResult<List<SubPermission>> querySubPermission(@RequestParam("menuKey") Long menuKey, @RequestParam("roleKey") Long roleKey){
        List<SubPermission> subPermissions = menuService.querySubPermission(menuKey, roleKey);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, subPermissions);
    }

    /**
     * Description:
     * <获取当前用户权限下的菜单>
     * @author wupanhua
     * @date 17:27 2019/8/6
     * @return JsonResult
     **/
    @GetMapping("currentMenu")
    @ApiOperation(value = "获取当前用户权限下的菜单",notes = "获取当前用户权限下的菜单")
    @ApiImplicitParam(name = "username",value = "用户名",paramType = "query",required = true)
    public JsonResult<Map<String, Object>> queryCurrentPermissionMenu() {
        Map<String, Object> map = menuService.queryCurrentPermissionMenu(SessionUtil.getRoles());
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, map);
    }
    /**
     * Description:
     * <根据id删除菜单>
     * @author wupanhua
     * @date 17:27 2019/8/6
     * @param id 1
     * @return JsonResult
     **/
    @DeleteMapping("deleteMenuById/{id}")
    @ApiOperation(value = "根据id删除菜单",notes = "根据id删除菜单")
    @ApiImplicitParam(name = "id",value = "菜单主键",dataType = "Long",paramType = "path",required = true)
    public JsonResult<Long> deleteMenuById(@PathVariable("id") Long id){

        menuService.deleteMenuById(id);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, id);
    }

    /**
     * Description:
     * <禁用/启用菜单>
     * @author pig
     * @date 17:32 2019/8/6
     * @param disableOrEnableMenuVo 1
     * @return JsonResult
     **/
    @PostMapping("disableOrEnableMenu")
    @ApiOperation(value = "禁用/启用菜单",notes = "禁用/启用菜单")
    @ApiImplicitParam(name = "disableOrEnableMenuVo",value = "启用或禁用菜单实体类",dataType = "DisableOrEnableMenuVo",paramType = "body",required = true)
    public JsonResult<Menu> disableOrEnableMenu(@RequestBody @Valid DisableOrEnableMenuVo disableOrEnableMenuVo){

        Menu menu = new Menu();
        BeanUtils.copyProperties(disableOrEnableMenuVo,menu);

        menuService.disableOrEnableMenu(menu);

        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, menu);
    }

    /**
     * Description:
     * <根据id查询菜单信息>
     * @author pig
     * @date 17:33 2019/8/6
     * @param id 1
     * @return JsonResult
     **/
    @GetMapping("queryMenuById")
    @ApiOperation(value = "根据id查询菜单信息",notes = "根据id查询菜单信息")
    @ApiImplicitParam(name = "id",value = "菜单主键",dataType = "Long",paramType = "query",required = true)
    public JsonResult<Menu> queryMenuById(Long id){

        Menu menu = menuService.queryMenuById(id);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS,menu);
    }

    /**
     * Description:
     * <根据id更新菜单信息>
     * @author pig
     * @date 17:33 2019/8/6
     * @param updateMenuVo 1
     * @return JsonResult
     **/
    @PostMapping("updateMenuById")
    @ApiOperation(value = "根据id更新菜单信息",notes = "根据id更新菜单信息")
    @ApiImplicitParam(name = "updateMenuVo",value = "更新菜单实体类",dataType = "UpdateMenuVo",paramType = "body",required = true)
    public JsonResult<Menu> updateMenuById(@RequestBody @Valid UpdateMenuVo updateMenuVo){

        Menu menu = new Menu();

        BeanUtils.copyProperties(updateMenuVo,menu);

        menuService.updateMenuById(menu);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS, menu);
    }

    /**
     * Description:
     * <查询所有的子菜单>
     * @author pig
     * @date 17:34 2019/8/6
     * @return JsonResult
     **/
    @GetMapping("AllChildren")
    @ApiOperation(value = "查询所有的子菜单",notes = "查询所有的子菜单")
    public JsonResult<List<Menu>> queryAllChildren(){

        List<Menu> menus = menuService.queryAllChildren();

        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS,menus);
    }

    /**
     * Description:
     * <批量删除菜单>
     * @author pig
     * @date 17:34 2019/8/6
     * @param keys 1
     * @return JsonResult
     **/
    @PostMapping("removeMenusBykeys")
    @ApiOperation(value = "批量删除菜单",notes = "批量删除菜单")
    @ApiImplicitParam(name = "keys",value = "菜单主键列表",dataType = "Long",paramType = "body",required = true,allowMultiple = true)
    public JsonResult<List<Menu>> removeMenusBykeys(@RequestBody List<Long> keys){

        List<Menu> menus = menuService.removeMenusBykeys(keys);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS,menus);
    }

    /**
     * Description:
     * <查询name是否重复>
     * @author wupanhua
     * @date 17:34 2019/8/6
     * @param name 1
     * @param id 2
     * @return JsonResult
     **/
    @GetMapping("menuVerifyName")
    @ApiOperation(value = "查询name是否重复",notes = "查询name是否重复")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "菜单名称",dataType = "String",paramType = "query",required = true),
            @ApiImplicitParam(name = "id",value = "菜单主键",dataType = "Long",paramType = "query",required = true)})
    public JsonResult<Boolean> menuVerifyName(@RequestParam("name") String name,@RequestParam("id") Long id){

        Boolean flag = menuService.menuVerifyName(name, id);
        return JsonResult.getInstant(ReturnCodeEnum.SUCCESS,flag);
    }
}
